﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace UI
{
    /// <summary>
    /// 充值送礼、累计消费、累计充值  招募送礼
    /// </summary>
    public class UIActivityRechargeSurprise : UIActivityTemplate
    {
        public UITexture uiTexture;
        public UISprite uiSprite;
        public UILabel labActivityTime;
        public UIScrollView scrollView;
        public SimpleGrid simpleGrid;
        public GameObject activityItemPref;
        public UILabel topTips;
        public ActivityType type;
        int[] canGet = new int[5];
        protected override void PrepareData()
        {
            activityType = type;
            switch (type)
            {
                case ActivityType.RechargeSurprise:
                    topTips.text = TableTextBlo.GetText("recharge_once_activity_tip");
                    //uiTexture.mainTexture = ;
                    break;
                case ActivityType.CostTotal:
                    topTips.text = TableTextBlo.GetText("CostTotal_award_diamond_tip");
                    break;
                case ActivityType.RechargeTotal:
                    topTips.text = TableTextBlo.GetText("ChargeTota_award_rmb_tip");
                    break;
                case ActivityType.WarriorRescruitTimes:
                    uiTexture.mainTexture = AssetLoad.ResourceManager.Instance.LoadResource("ActivityCopyTexture/UI_vip_di_01") as Texture2D;
                    uiSprite.spriteName = "UI_activity_item_yasina2";
                    uiSprite.MakePixelPerfect(); 
                    topTips.text = TableTextBlo.GetText("WarriorRescruitTimes_award_tip");
                    break;
            }
        }

        public override void Show()
        {
            base.Show();
            CSActivityInfo msg = new CSActivityInfo();
            msg.activityId = this.activityId;
            NetAPI.SendHttp(OpDefine.CSActivityInfo, msg, MsgCallback);
        }

        void MsgCallback(NetWork.Packets.Packet packet, bool success)
        {
            if (success)
            {
                SCActivityInfo msg = packet.kBody as SCActivityInfo;

                activityId = msg.activityId;

                //时间
                System.DateTime endTime = ServerTimerTool.Java2CSTimeLocale(msg.endTime);
                System.DateTime startTime = ServerTimerTool.Java2CSTimeLocale(msg.startTime);
                string endTimeStr = endTime.Month.ToString() + TableTextBlo.GetText("month") + endTime.Day.ToString() + TableTextBlo.GetText("day_2")
                    + string.Format("{0:d2}:{1:d2}:{2:d2}", endTime.Hour, endTime.Minute, endTime.Second);
                string startTimeStr = startTime.Month.ToString() + TableTextBlo.GetText("month") + startTime.Day.ToString() + TableTextBlo.GetText("day_2")
                    + string.Format("{0:d2}:{1:d2}:{2:d2}", startTime.Hour, startTime.Minute, startTime.Second);
                labActivityTime.text = startTimeStr + TableTextBlo.GetText("to") + endTimeStr;

                UpdateActivityItems(msg.itemList);
            }
        }

        void UpdateActivityItems(List<ActivityItem> itemList)
        {
            foreach (var item in simpleGrid.childs)
            {
                Destroy(item);
            }
            simpleGrid.childs.Clear();
            ResetCanGetNum();
            for (int i = 0; i < itemList.Count; i++)
            {
                GameObject obj = Instantiate(activityItemPref);
                obj.SetActive(true);
                simpleGrid.AddChild(obj);
                var script = obj.GetComponent<AcRechargeSingleItem>();
                script.type = type;
                script.m_callback = this.UpdateActivityItems;
                script.m_addNum = AddCanGetNum;
                script.SetActivityItem(itemList[i], activityId);
            }
            SetRedByNum();
            scrollView.ResetPosition();
            if (itemList.Count < 3)
                scrollView.enabled = false;
            else
                scrollView.enabled = true;
        }

        public void AddCanGetNum()
        {
            switch (type)
            {
                case ActivityType.RechargeSurprise:
                    canGet[0]++;
                    break;
                case ActivityType.CostTotal:
                    canGet[1]++;
                    break;
                case ActivityType.RechargeTotal:
                    canGet[2]++;
                    break;
                case ActivityType.WarriorRescruitTimes:
                    canGet[3]++;
                    break;
            }
        }

        public void ResetCanGetNum()
        {
            for (int i = 0; i < canGet.Length; i++)
                canGet[i] = 0;
        }

        public void SetRedByNum()
        {
            switch (type)
            {
                case ActivityType.RechargeSurprise:
                    if (canGet[0] < 1)
                    {
                        StaticData.redTips.SetPointState(RedPointTip.Red.rechargeSingle, 0);
                        StaticData.redTips.ReduceActivitys();
                    }
                    break;
                case ActivityType.CostTotal:
                    if (canGet[1] < 1)
                    {
                        StaticData.redTips.SetPointState(RedPointTip.Red.costTotal, 0);
                        StaticData.redTips.ReduceActivitys();
                    }
                    break;
                case ActivityType.RechargeTotal:
                    if (canGet[2] < 1)
                    {
                        StaticData.redTips.SetPointState(RedPointTip.Red.rechargeTotal, 0);
                        StaticData.redTips.ReduceActivitys();
                    }
                    break;
                case ActivityType.WarriorRescruitTimes:
                    if(canGet[3] <1)
                    {
                        StaticData.redTips.SetPointState(RedPointTip.Red.warriorRescruitTimes, 0);
                        StaticData.redTips.ReduceActivitys();
                    }
                    break;
            }
        }

    }
}